layui.use(['layer','upload','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var upload = layui.upload;
   window.layedit = layui.layedit;

    //获取companyId
    var _companyId = $.cookie("companyId");
    $.get("/company/" + _companyId,function (data) {
        $("#id").val(data.id);
        $("#companeName").val(data.companeName);
        $("#tel").val(data.tel);
        $("#email").val(data.email);
        $("#address").val(data.address);
        $("#webAddress").val(data.webAddress);
        $("#blRegiserNo").val(data.blRegiserNo);
        $("#blAddress").val(data.blAddress);
        $("#blDate").val(data.blDate);
        $("#blCourse").val(data.blCourse);
        $("#organizationalCode").val(data.organizationalCode);

        //法人资格证书
        $("#qualificationCertificate").val(data.qualificationCertificate);
        if (data.qualificationCertificate != null && data.qualificationCertificate != "") {
            $('#showImg').html("");
            $('#showImg').append('<img style="padding: 10px" width="30%" src="'+ data.qualificationCertificate +'" class="layui-upload-img">');
        }

        //企业荣誉
        $("#honors").val(data.honors);
        if (data.honors != null && data.honors != "") {
            var picArr = data.honors.split(";");
            for (var i = 0;i < picArr.length -1 ;i++) {
                $('#showImg2').append('<img style="padding: 10px" width="30%" src="'+ picArr[i] +'" class="layui-upload-img">');
            }
        }

        //企业介绍
        $("#layedit_content").html(data.introduce);
        //创建一个编辑器
        window.editIndex = layedit.build('layedit_content',{
            height : 535,
            uploadImage : {
                url : "/layedit/upload/pic"
            }
        });
        layedit.setContent(editIndex,data.introduce);
        //用于同步编辑器内容到textarea
        layedit.sync(editIndex);
    });

    //图片上传
    upload.render({
        elem: '#uploadImg'
        ,url: '/upload/pic'
        // ,multiple: true
        ,field:"uploadFile"
        ,before: function(obj){
        }
        ,done: function(res){
            //上传完毕
            var url = res.url;
            $('#showImg').html('');
            $("#qualificationCertificate").val('');
            $('#showImg').append('<img style="padding: 10px" width="30%" src="'+ url +'" class="layui-upload-img">');
            $("#qualificationCertificate").val(url);
        }
    });

    //图片上传
    var flag = true;
    $("#uploadImg2").click(function () {
        if (flag) {
            flag = false;
            $('#showImg2').html('');
            $("#honors").val('');
        }
    });
    upload.render({
        elem: '#uploadImg2'
        ,url: '/upload/pic'
        ,multiple: true
        ,field:"uploadFile"
        ,before: function(obj){
        }
        ,done: function(res){
            //上传完毕
            var url = res.url;
            /*$('#demo2').html('');*/
            $('#showImg2').append('<img style="padding: 10px" width="30%" src="'+ url +'" class="layui-upload-img">');
            $("#honors").val($("#honors").val() + url + ";");
        }
    });

    form.verify({
        content: function(value) {
            return layedit.sync(editIndex);
        }
    });


    //修改企业信息
    form.on('submit(edit)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //初始化数据
        $.ajax({
            url: '/company/edit',
            data: data.field,
            type: 'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});

